<template>
  <el-dialog :visible.sync="visible" width="50%" custom-class="default-dialog" :close-on-click-modal="false"
    @close="cancel" append-to-body v-dialogDrags>
    <div slot="title">
      <img v-if="type" :src="require(`@/assets/img/${type}.png`)" alt="">
      <span>{{ title }} </span>
    </div>
    <el-form :model="form" :rules="rules" ref="form" label-width="120px" class="dialog-form" :disabled="type == 'view'">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="站点名称" prop="hydroStationId">
<!--            <el-input v-model="form.name" placeholder="请输入断面名称"></el-input>-->
            <el-select size="small" v-model="form.hydroStationId" filterable placeholder="请选择">
              <el-option
                v-for="item in jclxOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="水量值" prop="waterAmount">
            <el-input type="number" v-model="form.waterAmount" placeholder="请输入监测值"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="监测日期" prop="date">
            <el-date-picker
              v-model="form.date"
              type="month"
              format="yyyy-MM"
              value-format="yyyy-MM"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer">
      <el-button size="small" @click="cancel">取 消</el-button>
      <el-button size="small" v-if="type != 'view'" type="primary" @click="sure">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  data() {
    return {
      visible: false,
      title: '',
      jclxOptions: [],
      form: {
      },
      rules: {
        date: [{ required: true, message: '请输入监测日期', trigger: 'blur' }],
        hydroStationId: [{ required: true, message: '请输入站点名称', trigger: 'blur' }],
        waterAmount: [{ required: true, message: '请输入水量', trigger: 'blur' }]
      },
      type: ''
    };
  },
  mounted() {
    this.getparams();
  },
  methods: {
    getparams(){
      this.$api.getWaterstationName({ keywords:""}).then((res) => {
        if(res && res.code == 200){
          this.jclxOptions = res.data || []
        }
      })
    },
    showModal(type, row) {
      this.visible = true
      this.type = type
      if (type == 'add') {
        this.title = '新增'
        this.form = {
        }
      } else if (type == 'edit') {
        this.title = '编辑'
        this.getInfoInit(row.id)
      } else if (type == 'view') {
        this.title = '详情'
        this.getInfoInit(row.id)
      }
    },
    // 详情
    getInfoInit(id) {
      this.$api.getWaterMonitor({ id: id }).then(res => {
        if (res && res.code == 200) {
          let data = res.data || {}
          //this.form = data
         /* this.jclxOptions.forEach(val => {  //遍历接口返回的id数组
            if (val.id=== data.hydroStationId) {
              data.name=val.name
            }
          })*/
          this.form = data
          this.form.id=id
        }
      })
    },
    // 确定
    sure() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.type == 'add') {
            this.$api.addWaterMonitor(this.form).then(res => {
              if (res && res.code == 200) {
                this.$notify.success({
                  title: '成功',
                  message: '数据保存成功'
                });
                this.visible = false
                this.$emit('listInit')
              }
            })
          } else if (this.type == 'edit') {
            this.$api.updateWaterMonitor(this.form).then(res => {
              if (res && res.code == 200) {
                this.$notify.success({
                  title: '成功',
                  message: '数据保存成功'
                });
                this.visible = false
                this.$emit('listInit')
              }
            })
          }
        }
      });
    },
    // 取消
    cancel() {
      this.visible = false;
      this.$refs["form"].resetFields();
    },
  }
};
</script>
<style lang="less" scoped>
.el-form-item__label {
  width: 150px;
}
</style>
